package com.liwenfeng.ui;

import com.liwenfeng.MyStream;
import com.liwenfeng.domain.Account;

import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

public class UITest {
    private Account account = new Account(2, "张三", 88.0);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Function<Integer, Integer> function = a -> {
            return a + 10;
        };
//        Integer reduce = MyStream.of(List.of(1, 2, 2, 3, 3, 5, 6, 6, 7, 8,4))
//                .map(item->1)
//                .reduce(0, Integer::sum);
//        System.out.println(reduce);
//                .forEach(System.out::println);
//        JFrame frame = new JFrame();
//        JButton button = new JButton("点我");
//        button.setSize(50,30);
//        button.addActionListener(e -> System.out.println("点击了按钮"));
//        frame.add(button);
//
//        frame.setSize(300,200);
//        frame.setVisible(true);
//        MyStream.of(List.of(1, 2, 3, 4, 5, 6))
//                .collect(ArrayList::new, item -> item + a)
//                .forEach(System.out::println);
//                .reduce((item, p) -> item + p, 0);
//                .forEach(System.out::println);

//        new UITest().change();
        MyStream.of(List.of(
                        List.of(
                                new Account(1, "JACK", 88.0),
                                new Account(2, "NICK", 88.0),
                                new Account(3, "XIAOMING", 88.0),
                                new Account(4, "LISI", 58.0)
                        ),
                        List.of(
                                new Account(1001, "rose", 1200.00),
                                new Account(1001, "rose", 1200.00),
                                new Account(1002, "jnker", 2100.00),
                                new Account(1003, "aaaa", 1800.00)
                        )
                ))
                .flatMap(MyStream::of)
//                .map(item -> {
//                    System.out.println(Thread.currentThread().getName());
//                    item.setName("zhangsan");
//                    return item;
//                })
                .sorted(Comparator.comparing(Account::getMoney).thenComparing(item -> item.getName().length()))
                .parallel(false)
                .distinct()
                .forEach(item -> {
                    long start = System.currentTimeMillis();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName()+"======="+item);
                    System.out.println(System.currentTimeMillis()-start);
                });

//                .collect(ArrayList<Account>::new, Collection::add);
//        for (Account account : collect) {
//            System.out.println(account);
//        }
    }

    public void change() throws ExecutionException, InterruptedException {
        MyStream.of(List.of(account))
                .map(r -> {
                    r.setId(100);
                    account.setName("李四");
                    return r;
                })
                .forEach(System.out::println);
    }
}
